package meta.api.web.account.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import meta.api.web.account.businessinfo.UserInfo;

@Repository
public class UserMngDao {

    @Autowired
    private SqlSessionTemplate sqlSession;

    private static final String NAMESPACE = "meta.api.web.account.dao.userMng.";

    public boolean checkExistAccount(String account) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkExistAccount", params);
        return result > 0 ? true : false;
    }

    public boolean checkExistAccountId(String id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkExistAccountId", params);
        return result > 0 ? true : false;
    }

    public boolean checkAccountUsedByOther(String id, String account) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("account", account);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkAccountUsedByOther", params);
        return result > 0 ? true : false;
    }

    public int getUserTotal(String query) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("query", query);
        return sqlSession.selectOne(NAMESPACE + "getUserTotal", params);
    }

    public List<UserInfo> getUserList(String query, Integer current, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("query", query);
        if (current != null && pageSize != null) {
            params.put("current", pageSize * (current - 1));
            params.put("pageSize", pageSize);
        } else {
            params.put("current", null);
            params.put("pageSize", null);
        }
        return sqlSession.selectList(NAMESPACE + "getUserList", params);
    }

    public void registerUser(UserInfo user) {
        sqlSession.insert(NAMESPACE + "registerUser", user);
    }

    public UserInfo getUser(String account, boolean pswdFlg) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        params.put("pswdFlg", pswdFlg);
        return sqlSession.selectOne(NAMESPACE + "getUser", params);
    }

    public void updateUser(UserInfo user) {
        sqlSession.update(NAMESPACE + "updateUser", user);
    }

    public void deleteUser(String id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        sqlSession.delete(NAMESPACE + "deleteUser", params);
    }

    public boolean checkLogin(String account, String password) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        params.put("password", password);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkLogin", params);
        return result > 0 ? true : false;
    }

    public void updatePassword(String account, String password) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        params.put("password", password);
        sqlSession.update(NAMESPACE + "updatePassword", params);
    }

    public void updateEmail(UserInfo user) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", user.getId());
        params.put("account", user.getAccount());
        params.put("username", user.getUsername());
        params.put("email", user.getEmail());
        sqlSession.update(NAMESPACE + "updateEmail", params);
    }

    public boolean checkExistRole(String roleNm) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roleNm", roleNm);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkExistRole", params);
        return result > 0 ? true : false;
    }

    public void insertRole(String roleNm) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roleNm", roleNm);
        sqlSession.insert(NAMESPACE + "insertRole", params);
    }
}
